'use strict';

var React = require('react/addons');
var Router = require('react-router'); 
var RouteHandler = Router.RouteHandler;


var RadioGroupComponent = React.createClass({
  getInitialState : function(){
    var defaultSelectedItem = null;
     this.props.children.map(function(item, i) {
        if(item.props.defaultRadio)
          defaultSelectedItem = item;
     });
      return {
        selectedItem: defaultSelectedItem
      };
  },
 selectItem: function(item) {
    this.setState({
      selectedItem: item
    });
  },
  clearState : function(){
    this.setState({
      selectedItem: null
    });
  },
  getCruSelect : function(){
    return this.state.selectedItem;
  },
  render: function() {
    var selectedKey = (this.state.selectedItem && this.state.selectedItem.props.value) || null;
    var children = this.props.children.map(function(item, i) {
      var isSelected = item.props.value === selectedKey;
      return React.addons.cloneWithProps(item, {
        isSelected: isSelected,
        selectItem: this.selectItem,
        key : i
      });
    }, this);

    return (
      /* jshint ignore:start */
          <div className="do-radio-group">
            { children }
          </div>
      /* jshint ignore:end */
    );
  }
});

module.exports = RadioGroupComponent;
